package ch.csp.io;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;

import ch.csp.exchange.ParagraphWithWords;
import ch.csp.transformer.ColumnedLineTransformer;
import ch.csp.transformer.File2ParagraphWithWords;

public class FileReaderWriter {

	private final String inputFile;
	private final String outputFile;
	final static Charset ENCODING = StandardCharsets.UTF_8;

	public FileReaderWriter(String inputFile, String outputFile) {
		this.inputFile = inputFile;
		this.outputFile = outputFile;
	}
	
	public ArrayList<ParagraphWithWords> readFile(){
		//Claudio
		File2ParagraphWithWords transformer = new File2ParagraphWithWords(this.inputFile);
		ArrayList<ParagraphWithWords> paragraphsWithWords = transformer.transform();
		return paragraphsWithWords;
	}

	public void writeFile(ArrayList<String> columnedLines) {
		//Philipp: Schreibe Datei
		Path path = Paths.get(this.outputFile);
	    try (BufferedWriter writer = Files.newBufferedWriter(path, ENCODING)){
	      for(String line : columnedLines){
	        writer.write(line);
	        writer.newLine();
	      }
	    }catch(IOException io){
	    	io.printStackTrace();
	    }
	}

}
